import { _decorator, Component, error, instantiate, Label, Node, Prefab, ProgressBar } from 'cc';
import { GameManager } from './GameManager';
import { Card, CardImage, CARDTYPE, drawCardNeedEX } from '../AshFramework/Datas/GameConstant';
import { MenuManager } from './MenuManager';
import { ZTool } from '../AshFramework/Utils/ZTool';
import { ProbabilityWindow } from './GameUI/ProbabilityWindow';
import { CardShow } from './CardShow';
import { CardController } from './CardController';
import { EventManager, MyEvent } from '../AshFramework/Managers/EventManager';
import { GameUtil } from './GameUtil';
const { ccclass, property } = _decorator;

@ccclass('DrawCardController')
export class DrawCardController extends Component {

    @property(Node)
    CardBox: Node = null;

    @property(Label)
    DrawCardGrade: Label = null;

    @property(ProgressBar)
    EXProgressBar: ProgressBar = null;

    @property(Label)
    NeedEX: Label = null;

    @property(Label)
    HaveEX: Label = null;

    @property(Node)
    ProbabilityNode: Node = null;

    @property(Node)
    PropertyNode: Node = null;

    @property(Prefab)
    DrawCardPrefab: Prefab = null;//抽奖卡片

    @property(Node)
    DrawCardNode: Node = null;

    @property(Node)
    DrawCardParentNode: Node = null;

    @property(Prefab)
    CardPrefab: Prefab = null;//玩家卡片预制体

    @property(Node)
    CardParentNode: Node = null;

    @property(Node)
    CloseBtn: Node = null;

    private _targetWindow: Node = null;
    private _arrTemporaryCard: Array<Node> = new Array();
    private _arrCard: Array<Node> = new Array();

    protected onLoad(): void {
        this.showCard();
    }

    DrawOnce() {
        if (GameManager.Instance.userData.masonry < 100) {
            GameUtil.showHint("砖石不够");
            return;
        }
        MenuManager.Instance.addMasonry(-100);
        //抽奖画面
        // this.CardParentNode.removeAllChildren();
        this.clearTemporaryCard();
        this.DrawCardNode.active = true;
        this.createCard();
    }

    DrawRepeat() {
        if (GameManager.Instance.userData.masonry < 950) {
            GameUtil.showHint("砖石不够");
            return;
        }
        MenuManager.Instance.addMasonry(-950);
        //抽奖画面
        this.clearTemporaryCard();
        this.DrawCardNode.active = true;
        this.CloseBtn.active = false;
        let count = 0;
        this.schedule(() => {
            this.createCard();
            count++;
            if (count == 9) {
                this.CloseBtn.active = true;
            }
        }, 0.2, 9);
    }

    //#region 抽卡实现
    createCard() {
        GameManager.Instance.userData.drawCardEX++;
        this.updateEX()
        //卡片的类型
        let rand = ZTool.GetRandomInt(0, 100);
        let type: CARDTYPE;
        //卡片类型的最后编号
        let lastNumber: number;
        //更新卡牌出现的概率
        ProbabilityWindow.Instance.updateProbability();
        if (rand < ProbabilityWindow.Instance.probability4) {
            type = CARDTYPE.CARD_GOLDEN;
            lastNumber = CardImage[type].length;
        } else if (rand < ProbabilityWindow.Instance.probability3) {
            type = CARDTYPE.CARD_PURPLE;
            lastNumber = CardImage[type].length;
        } else if (rand < ProbabilityWindow.Instance.probability2) {
            type = CARDTYPE.CARD_BLUE;
            lastNumber = CardImage[type].length;
        } else {
            type = CARDTYPE.CARD_GRAY;
            lastNumber = CardImage[type].length;
        }
        //卡片编号
        let num: number = ZTool.GetRandomInt(0, lastNumber);
        //检查该卡片玩家是否已经拥有
        let card: Card | null = this.checkCard(type, num);
        let isNew: boolean = false;
        if (!card) {
            card = {
                type: type,
                number: num,
                grade: 1,
                haveCount: 0,
            } as Card;
            GameManager.Instance.userData.card.push(card);
            isNew = true;
        } else {
            card.haveCount++;
        }
        let cardNode: Node = instantiate(this.DrawCardPrefab);
        this.DrawCardParentNode.addChild(cardNode);
        this._arrTemporaryCard.push(cardNode);
        cardNode.getComponent(CardShow).initCard(card);
        if (isNew) {
            this.addCard(card);
        } else {
            EventManager.Scene.emit(MyEvent.CARD_STATE, card);
        }
    }

    checkCard(type: CARDTYPE, num: number): Card | null {
        for (const card of GameManager.Instance.userData.card) {
            if (card.type === type && card.number == num) return card;
        }
        return null;
    }

    clearTemporaryCard() {
        for (let i = 0; i < this._arrTemporaryCard.length; i++) {
            if (this._arrTemporaryCard[i]) {
                this._arrTemporaryCard[i].destroy();
            }
        }
        this._arrTemporaryCard.length = 0;
    }

    //更新 显示玩家所有卡片信息
    showCard() {
        let arrCards = GameManager.Instance.userData.card;
        for (let i = 0; i < arrCards.length; i++) {
            if (arrCards[i]) {
                let card: Card = {
                    type: arrCards[i].type,
                    number: arrCards[i].number,
                    grade: arrCards[i].grade,
                    haveCount: arrCards[i].haveCount,
                }
                let cardNode: Node = instantiate(this.CardPrefab);
                this.CardParentNode.addChild(cardNode);
                cardNode.getComponent(CardController).initCard(card);
            }
        }
    }

    //添加一张卡牌
    addCard(card: Card) {
        let cardNode: Node = instantiate(this.CardPrefab);
        this.CardParentNode.addChild(cardNode);
        cardNode.getComponent(CardController).initCard(card);
    }


    DrawCardGradeBtn() {
        //跳出召唤概率界面
        if (this._targetWindow && this._targetWindow.active) {
            this._targetWindow.active = false;
        }
        this._targetWindow = this.ProbabilityNode;
        this._targetWindow.active = true;
    }

    PropertyBtn() {
        //跳出召唤属性界面
        if (this._targetWindow && this._targetWindow.active) {
            this._targetWindow.active = false;
        }
        this._targetWindow = this.PropertyNode;
        this._targetWindow.active = true;
    }

    updateGrade() {
        this.DrawCardGrade.string = String(GameManager.Instance.userData.drawCardGrade);
    }

    updateEX() {
        let needNum = drawCardNeedEX[GameManager.Instance.userData.drawCardGrade];
        let haveNum = GameManager.Instance.userData.drawCardEX;
        if (haveNum / needNum >= 1) {
            GameManager.Instance.userData.drawCardGrade++;
            haveNum -= needNum
            GameManager.Instance.userData.drawCardEX = haveNum;
            needNum = drawCardNeedEX[GameManager.Instance.userData.drawCardGrade];
            this.updateGrade();
        }
        this.HaveEX.string = String(haveNum);
        this.NeedEX.string = String(needNum);
        this.EXProgressBar.progress = haveNum / needNum;
    }

    protected onEnable(): void {
        this.updateGrade();
        this.updateEX();
    }
}


